package com.zts.modules.sys.mapper;

import com.google.common.base.Strings;
import com.zts.modules.sys.entity.User;
import org.apache.ibatis.jdbc.SQL;

/**
 * Created by Zhangkh on 2017/6/2.
 */
public class UserSqlProvider {
    public String get(final Long id) {
        StringBuilder sb = new StringBuilder();
        sb.append(" select u.* from sys_user u ");
        sb.append("  where u.id = " + id);
        return sb.toString();

    }

    public String getByEntity(final User user) {
        return new SQL() {
            {
                SELECT("u.* ");
                FROM(" sys_user u  ");

                //通过条件 判断是否需要更新该字段
                if (!Strings.isNullOrEmpty(user.getUserName())) {
                    WHERE("u.user_name = #{userName}");
                }
                if (!Strings.isNullOrEmpty(user.getPhone())) {
                    WHERE("u.phone = #{phone}");
                }
                if (!Strings.isNullOrEmpty(user.getEmail())) {
                    WHERE("u.email = #{email}");
                }
                if (!Strings.isNullOrEmpty(user.getIdcard())) {
                    WHERE("u.idcard = #{idcard}");
                }

                if (null != user.getId() && user.getId() > 0) {
                    WHERE("u.id  = #{id}");
                }
                if (!Strings.isNullOrEmpty(user.getSex())) {
                    WHERE("u.sex = #{sex}");
                }
                if (!Strings.isNullOrEmpty(user.getType())) {
                    WHERE("u.type = #{type}");
                }
                if (!Strings.isNullOrEmpty(user.getMobile())) {
                    WHERE("u.mobile = #{mobile}");
                }
                if (!Strings.isNullOrEmpty(user.getStatus())) {
                    WHERE("u.status = #{status}");
                }
                if (!Strings.isNullOrEmpty(user.getPhoneShort())) {
                    WHERE("u.phone_short = #{phoneShort}");
                }
                if (!Strings.isNullOrEmpty(user.getMobileShort())) {
                    WHERE("u.mobile_short = #{mobileShort}");
                }
                if (!Strings.isNullOrEmpty(user.getLoginName())) {
                    WHERE("u.login_name = #{loginName}");
                }

                ORDER_BY(" u.id ");
            }
        }.toString();
    }

    /**
     * 拼装 getList sql语句
     *
     * @param user
     * @return
     */
    public String getList(final User user) {
        return getByEntity(user);
    }

    public String update(final User user) {
        return new SQL() {
            {
                UPDATE("sys_user ");
                if (!Strings.isNullOrEmpty(user.getUserName())) {
                    SET("user_name = #{userName}");
                }
                if (!Strings.isNullOrEmpty(user.getStatus())) {
                    SET("status = #{status}");
                }
                if (!Strings.isNullOrEmpty(user.getPhone())) {
                    SET("phone = #{phone}");
                }
                if (!Strings.isNullOrEmpty(user.getPhoneShort())) {
                    SET("phone_short = #{phoneShort}");
                }
                if (!Strings.isNullOrEmpty(user.getMobile())) {
                    SET("mobile = #{mobile}");
                }
                if (!Strings.isNullOrEmpty(user.getMobileShort())) {
                    SET("mobile_short = #{mobileShort}");
                }

                if (!Strings.isNullOrEmpty(user.getEmail())) {
                    SET("email = #{email}");
                }

                if (!Strings.isNullOrEmpty(user.getIdcard())) {
                    SET("idcard = #{idcard}");
                }
                if (!Strings.isNullOrEmpty(user.getSex())) {
                    SET("sex = #{sex}");
                }

                SET("update_time = now()");

                if (!Strings.isNullOrEmpty(user.getRemark())) {
                    SET("remark = #{remark}");
                }

                if (!Strings.isNullOrEmpty(user.getGrade())) {
                    SET("grade = #{grade}");
                }

                if (!Strings.isNullOrEmpty(user.getType())) {
                    SET("type = #{type}");
                } else {
                    SET("type = '1'");
                }

                WHERE("id = #{id}");
            }
        }.toString();
    }

    public String insert(final User user) {
        return new SQL() {{
            INSERT_INTO("sys_user");
            VALUES("login_name", "#{loginName}");
            VALUES("user_name", "#{userName}");
            VALUES("password", "#{password}");

            if (!Strings.isNullOrEmpty(user.getStatus())) {
                VALUES("status", "#{status}");
            } else {
                VALUES("status", "'1'");
            }

            if (!Strings.isNullOrEmpty(user.getType())) {
                VALUES("type", "#{type}");
            } else {
                VALUES("type", "'1'");
            }

            VALUES("sex", "#{sex}");
            VALUES("grade", "#{grade}");

            VALUES("phone", "#{phone}");
            VALUES("phone_short", "#{phoneShort}");
            VALUES("mobile", "#{mobile}");
            VALUES("mobile_short", "#{mobileShort}");
            VALUES("email", "#{email}");
            VALUES("idcard", "#{idcard}");
            VALUES("salt", "#{salt}");

            VALUES("create_time", "now()");
            VALUES("update_time", "now()");
            VALUES("remark", "#{remark}");
        }}.toString();
    }

    public String updatePassword(final User user) {
        return new SQL() {{
            UPDATE("sys_user ");
            if (!Strings.isNullOrEmpty(user.getPassword())) {
                SET("password = #{password}");
            }

            WHERE("id = #{id}");
        }}.toString();
    }

    public String delete(final User user) {
        return new SQL() {{
            UPDATE("sys_user");
            SET("status = '0' ");
            WHERE("id = #{id}");
        }}.toString();
    }

    public String getByLoginKey(String loginKey) {
        return new SQL() {
            {
                SELECT("u.*");
                FROM(" sys_user u  ");
                WHERE(" u.login_name = #{loginKey}");
            }
        }.toString();
    }

    public String getUserGroup() {
        StringBuilder sb = new StringBuilder();
        sb.append(" select g.id id ,g.group_name as name,g.parentid parentid,'0' as type,g.sort_num sort_num ");
        sb.append(" from sys_group g where g.status='1' ");
        sb.append(" union ");
        sb.append(" select u.id id,u.user_name as name,ug.group_id parentid,'1' as type,ug.sort_num sort_num  ");
        sb.append(" from sys_user u,sys_user_group ug ");
        sb.append(" where u.id=ug.user_id and ug.group_type='1' ");
        return sb.toString();
    }

    public String getGroups(String userIds) {
        SQL sql = new SQL() {
            {
                SELECT("ug.*,g.group_name ");
                FROM(" sys_user_group ug,sys_group g  ");
                WHERE(" g.id = ug.group_id and ug.user_id in ( " + userIds + " )");
                ORDER_BY("ug.user_id");
            }
        };
        return sql.toString();
    }


}
